#include "TeamHistoryMenu.h"
#include "PrintUtilities.h"
#include "StringUtilities.h"
#include "MenuManager.h"
#include "ServiceProvider.h"
#include "GamePrintUtils.h"
#include "TeamManager.h"
#include "Team.h"
#include "People.h"
#include "PeopleManager.h"
#include "LeagueManager.h"
#include "MatchData.h"
#include "League.h"

TeamHistoryMenu::TeamHistoryMenu(MenuManager* const i_menuManager)
	:Menu(i_menuManager)
{
}

TeamHistoryMenu::~TeamHistoryMenu(void)
{
}

void TeamHistoryMenu::Draw() const
{
	using STD::cout;
	using STD::endl;
	using namespace Print;

	cout << Print::Title("TEAM HISTORY");

	ShowTeamHistory();

	cout << endl;

	STL::string options = "0) Return to previous menu'.";

	cout << Print::Options(options) << "Selection: ";
}

bool TeamHistoryMenu::Input(const STL::string& i_input)
{
	bool goodInput = true;
	const int res = Utilities::StringToInt(i_input);

	switch(res)
	{
	case 0:
		GetMM()->PopMenu();
		break;
	};

	return goodInput;
}

void TeamHistoryMenu::ShowTeamHistory() const
{
	using namespace STD;
	using namespace Print;
	using namespace Utilities;

	const int maxTeamNameSize = Team::GetParams().MAX_CHAR_TEAM_NAME;
	
	Team* const team = GetSP()->GetTeamMgr().GetTeam(m_teamID);

	const TeamHistory& hist = team->GetTeamHistory();

	cout << "GENERAL HISTORY STATS" << endl;
	cout << endl;

	cout << TabUpTo("Name: ", maxTeamNameSize);
	cout << team->GetName() << endl;
	cout << TabUpTo("Bankrupt: ", maxTeamNameSize);
	cout << hist.GetBankruptCount() << endl;
	cout << endl;

	cout << TabUpTo("First position: ", maxTeamNameSize);
	cout << hist.GetPodium().at(0) << endl;

	cout << TabUpTo("Second position: ", maxTeamNameSize);
	cout << hist.GetPodium().at(1) << endl;

	cout << TabUpTo("Third position: ", maxTeamNameSize);
	cout << hist.GetPodium().at(2) << endl;
	cout << endl;

	for(size_t i = 0; i < hist.GetYearsInLeagueLayer().size(); ++i)
	{
		cout << TabUpTo("Years in league "+Utilities::IntToStlString(i)+": ", maxTeamNameSize);
		cout << hist.GetYearsInLeagueLayer().at(i) << endl;
	}

	cout << endl;
	cout << endl;
	cout << endl;

	static const int peopleNameSize = People::GetParams().MAX_CHAR_NAME - 5;

	cout << TabUpTo("YEAR", 6);
	cout << TabUpTo("LEAGUE", peopleNameSize);
	cout << TabUpTo("POS", 5);
	cout << TabUpTo("TOP SCORER PLAYER", peopleNameSize);
	cout << TabUpTo("GOL", 5);
	cout << TabUpTo("TOP MOM PLAYER", peopleNameSize);
	cout << TabUpTo("MOM", 5);
	cout << TabUpTo("TOP RATING PLAYER", peopleNameSize);
	cout << TabUpTo("RAT", 6);
	cout << endl;

	PrintDecorationLine(cout, '-');
	cout << "\n";

	for (size_t i = 0; i < hist.GetItems().size(); ++i)
	{
		cout << TabUpTo(hist.GetItems()[i].m_year, 6);
		cout << TabUpTo( GetSP()->GetLeagueMgr().GetLeagueFromID(hist.GetItems()[i].m_leagueID)->GetName().substr(0,peopleNameSize-1), peopleNameSize);
		cout << TabUpTo( hist.GetItems()[i].m_leaguePosition, 5);

		const People* const scoredPlayer = GetSP()->GetPeopleMgr().GetPeopleFromID(hist.GetItems()[i].m_topGoalPlayerID);
		cout << TabUpTo( (scoredPlayer ? scoredPlayer->GetName().substr(0,peopleNameSize-1) : STL::string("-----")) , peopleNameSize);
		cout << TabUpTo(hist.GetItems()[i].m_topGoal, 5);

		const People* const momPlayer = GetSP()->GetPeopleMgr().GetPeopleFromID(hist.GetItems()[i].m_topMomPlayerID);
		cout << TabUpTo( (momPlayer ? momPlayer->GetName().substr(0,peopleNameSize-1) : STL::string("-----")) , peopleNameSize);
		cout << TabUpTo(hist.GetItems()[i].m_topMom, 5);

		const People* const ratingPlayer = GetSP()->GetPeopleMgr().GetPeopleFromID(hist.GetItems()[i].m_topRatingPlayerID);
		cout << TabUpTo( (ratingPlayer ? ratingPlayer->GetName().substr(0,peopleNameSize-1) : STL::string("-----")) , peopleNameSize);
		cout << TabUpTo(EvalToStr(hist.GetItems()[i].m_topRating), 6);

		cout << endl;
	}
	
	cout << endl << endl;
}
